package dk.aau.walkietalkieui.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import dk.aau.walkietalkieui.R;
import dk.aau.walkietalkieui.message.MyMessage;

public class ChatMessageAdapter extends BaseAdapter {
	private ArrayList<MyMessage> messages;
	private Context context;

	public ChatMessageAdapter(Context context) {
		this.context = context;
		messages = new ArrayList<MyMessage>();
	}

	@Override
	public int getCount() {
		return messages.size();
	}

	@Override
	public MyMessage getItem(int position) {
		return messages.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ViewHolder {
		TextView tvChatId;
		TextView tvChat;
		TextView tvDate;
		//ImageView ivType;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			v = inflater.inflate(R.layout.message, null);
			ViewHolder holder = new ViewHolder();
			holder.tvChatId = (TextView) v.findViewById(R.id.tvChatId);
			holder.tvDate = (TextView) v.findViewById(R.id.tvChatDate);
			holder.tvChat = (TextView) v.findViewById(R.id.tvChat);
			v.setTag(holder);
		}
		MyMessage msg = messages.get(position);
		if (msg != null) {
			ViewHolder holder = (ViewHolder) v.getTag();

			holder.tvChatId.setText(msg.getNameOwn());
			holder.tvDate.setText(msg.getTime());
			switch(msg.getType()){
			case TYPE_RECORD :
				holder.tvChat.setText(R.string.click_to_hear_the_recording);
				break;
			case TYPE_PICTURE:
				holder.tvChat.setText(R.string.click_to_display_the_picture);
				break;
			default:
				String data = msg.getDataPath();
				holder.tvChat.setText(data);
				break;
			}
			
		}
		return v;
	}

	public void addMessage(MyMessage msg) {
		messages.add(msg);

	}

}
